package com.example.wan_android.viewmodel

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.wan_android.http.code.UserRepository
import com.example.wan_android.tools.Sp
import kotlinx.coroutines.launch

class RegisterViewModel : BaseViewModel() {
    var context: Context? = null
    private val _showPassword = MutableLiveData<Boolean>(false)
    private val _againShowPassword = MutableLiveData<Boolean>(false)
    val againShowPassword: MutableLiveData<Boolean> = _againShowPassword
    val showPassword: MutableLiveData<Boolean> = _showPassword
    private val _currentPosition = MutableLiveData<Int>(0)
    private val _againCurrentPosition = MutableLiveData<Int>(0)
    val currentPosition = _currentPosition
    val againCurrentPosition = _againCurrentPosition
    fun setShowPassword(
    ) {
        _showPassword.value = !(_showPassword.value ?: false)
    }

    fun setAgainShowPassword(
    ) {
        _againShowPassword.value = !(_againShowPassword.value ?: false)
    }

    fun setCurrentPosition(index: Int) {
        _currentPosition.value = index
    }

    fun setAgainCurrentPosition(index: Int) {
        _againCurrentPosition.value = index
    }

    fun register(username: String, password: String, againPassword: String) {
        viewModelScope.launch {
            UserRepository().fetchRegisterUserData(username, password, againPassword).onSuccess {
                Toast.makeText(context, "注册成功!", Toast.LENGTH_SHORT).show()
                finishActivity()
            }.onFailure {
                Toast.makeText(context, it.message + "", Toast.LENGTH_SHORT).show()
            }
        }
    }
}